package org.management.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.management.base.BaseServiceImpl;
import org.management.config.shiro.manager.TokenManager;
import org.management.system.dao.UserDao;
import org.management.system.dao.UserRoleDao;
import org.management.system.entity.RoleEntity;
import org.management.system.entity.UserEntity;
import org.management.system.entity.UserRoleEntity;
import org.management.system.service.RoleService;
import org.management.system.service.UserService;
import org.management.utils.DateUtils;
import org.management.utils.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.annotation.Resource;

/**
 * 功能描述
 * 用户服务类
 * @author jiangyan
 * @date 2019/03/14
 * @description
 * @project management
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements UserService {

	@Resource
	private UserDao userDao;

	@Resource
	private UserRoleDao userRoleDao;

	@Resource
	private RoleService roleService;

	/**
	 * 用户登录
	 */
	@Override
	public UserEntity login(String name, String password) {
		Map<String, Object> map = new HashMap<>(2);
		map.put("loginName", name);
		map.put("password", password);
		return this.userDao.login(map);
	}

	/**
	 * 查询用户名称是否唯一
	 */
	@Override
	public UserEntity selectByUsername(String userName) {
		Example example = new Example(UserEntity.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("loginName", userName);
		List<UserEntity> users = this.userDao.selectByExample(example);
		if (!users.isEmpty()) {
			return users.get(0);
		}
		return null;
	}

	/**
	 * 保存用户信息及关联角色集合
	 */
	public void saveUserWithRoles(String id, String roleIds) {
		String[] ids = roleIds.split(",");
		if (StringUtils.isNotEmpty(ids)) {
			for (String roleId : ids) {
				UserRoleEntity userRoleEntity = new UserRoleEntity();
				userRoleEntity.setUserId(id);
				userRoleEntity.setRoleId(roleId);
				int count = this.userRoleDao.selectCount(userRoleEntity);
				if (count == 0) {
					userRoleEntity.setId(UUID.randomUUID().toString());
					userRoleEntity.setUserId(id);
					userRoleEntity.setCreateTime(DateUtils.getCurDate());
					userRoleEntity.setLastUpdateTime(DateUtils.getCurDate());
					userRoleEntity.setCreateUser(TokenManager.getLoginName());
					userRoleEntity.setLastUpdateUser(TokenManager.getLoginName());
					userRoleEntity.setIsDelete(false);
					userRoleEntity.setIsDefault(false);
					this.userRoleDao.insertSelective(userRoleEntity);
				}
			}
			Map<String,Object> param = new HashMap<>();
			param.put("userId", id);
			param.put("roleIds", ids);
			this.userRoleDao.deleteUserWithRoleIds(param);
		}
	}

	/**
	 * 设置用户默认角色
	 */
	@Override
	public void setDefaultRole(String userId, String roleId) {
		UserRoleEntity userRole = new UserRoleEntity();
		userRole.setIsDefault(false);
		Example example = new Example(UserRoleEntity.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId",userId);
		this.userRoleDao.updateByExampleSelective(userRole, example);
		UserRoleEntity entity = new UserRoleEntity();
		entity.setUserId(userId);
		entity.setRoleId(roleId);
		entity = this.userRoleDao.selectOne(entity);
		if (entity != null) {
			entity.setIsDefault(true);
			this.userRoleDao.updateByPrimaryKeySelective(entity);
		}
	}

	/**
	 * 获取用户默认关联角色
	 */
	@Override
	public RoleEntity getDefaultRole(String userId) {
		UserRoleEntity entity = new UserRoleEntity();
		entity.setUserId(userId);
		entity.setIsDefault(true);
		entity = this.userRoleDao.selectOne(entity);
		if (entity == null) {
			return null;
		}
		return this.roleService.selectByKey(entity.getRoleId());
	}

}
